package com.bruce;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import javax.servlet.http.HttpSession;
import com.mysql.jdbc.Driver;

public class Member {
	String userid;
    String nickname;
    String pw1;
    String pw2;
    String email;
    String utxt; //留言內容
    String title;// 標題
    String dt; 
    

	private String useridMessage;
	private String nicknameMessage;
    private String pwMessage;
    private String emailMessage;
    private String loginMessage;
    private boolean login;
   
    public Member(){}
        
    public Member(String userid, String pw1) {
        super();
        this.userid = userid;
        this.pw1 = pw1;
    }

    public Member(String userid, String pw1, String pw2, String email) {
        super();
        this.userid = userid;
        this.pw1 = pw1;
        this.pw2 = pw2;
        this.email = email;
    }
        
    public Member(String userid, String nickname, String pw1, String pw2, String email) {
        super();
        this.userid = userid;
        this.nickname = nickname;
        this.pw1 = pw1;
        this.pw2 = pw2;
        this.email = email;
    }
        
    public boolean login(){
    	Connection conn = null;
        PreparedStatement pstmt = null;
        login = false;
        try {
	        DriverManager.registerDriver(new Driver());
	        conn = DriverManager.getConnection("jdbc:mysql://j.snpy.org/j102", "jstu", "abc123");
	        String sql = "select * from users where id=? and password=?";
	        pstmt = conn.prepareStatement(sql);
	        pstmt.setString(1, userid);
	        pstmt.setString(2, pw1);
	        ResultSet rs = pstmt.executeQuery();
	            
            if 	(rs.next()){
                login = true;
                nickname = rs.getString("nickname");
                email = rs.getString("email");
            }else{
                loginMessage = "登入失敗";
	        }
        } catch (SQLException e) {
        	e.printStackTrace();
        } finally{
            if 	(conn!=null){
	            try	{
	                pstmt.close();
	                conn.close();
	            } catch (SQLException e) { 
	            	e.printStackTrace();
	            }
            }
        }
        return login;
    }
 
    public boolean validate(){ 
        boolean valid = false; 
        if 	(userid.length()>=4 && userid.length()<=20)
        {	if 	(nickname.length()>=4 || nickname.length()<=20)
        	{	if 	(pw1.length()>0)
        		{	if 	(pw1.equals(pw2))
        			{	if 	(email.indexOf("@")>0)
        				{	valid = true; //全部正確才給true
        				}else emailMessage = "電子郵件格式錯誤";       			
        			}else pwMessage = "密碼不符合";
        		}else pwMessage = "密碼1長度不符合";
        	}else nicknameMessage = "暱稱長度不符合";
        }else useridMessage = "userid長度不符合";
        
//        System.out.println(useridMessage);
//        System.out.println(nicknameMessage);
//        System.out.println(pwMessage);
//        System.out.println(emailMessage);      	

        
        if	(valid){ 
            try {	DriverManager.registerDriver(new Driver()); 
                    Connection conn = DriverManager.getConnection("jdbc:mysql://j.snpy.org/j102", "jstu", "abc123");
                    String sql = "SELECT * users WHERE id LIKE userid" ; 
		            PreparedStatement pstmt = conn.prepareStatement(sql);
		            ResultSet rs = pstmt.executeQuery();
		            System.out.println("找到了,共有 ");
		            if (!rs.next())
                    {	System.out.println("找不到!");
                    	
                    }else 
                    {	
			            sql = "INSERT INTO users(id, nickname, password, email) values(?,?,?,?)"; 	            
			            pstmt.setString(1, userid); 
			            pstmt.setString(2, nickname); 
			            pstmt.setString(3, pw1); 
			            pstmt.setString(4, email); 
			            int rowCount = pstmt.executeUpdate();
			            System.out.println(rowCount); //輸出rowCount檢查
                    }
            } catch (SQLException e) {
                    e.printStackTrace(); 
            }  
        }
        return valid;
    }
 
        
    private void saveOld() {
        try {	DriverManager.registerDriver(new Driver()); 
            	Connection conn = DriverManager.getConnection("jdbc:mysql://j.snpy.org/j102", "jstu", "abc123"); 
            	Statement stmt = conn.createStatement(); 
            	String sql = "Insert into users(id, nickname, password, email) values ('"+userid+"','"+nickname+"','"+pw1+"','"+email+"')";
                System.out.println(sql); 
                int rowCount = stmt.executeUpdate(sql); 
                System.out.println(rowCount); 
        } catch (SQLException e) {  
                e.printStackTrace(); 
        }
    }
        public String getUserid() { 
                return userid; 
        }
        
        public void setUserid(String userid) { 
                this.userid = userid; 
        }
 
        public String getPw1() { 
                return pw1; 
        }
        
        public void setPw1(String pw1) { 
                this.pw1 = pw1; 
        }
 
        public String getPw2() { 
                return pw2; 
        }
 
        public void setPw2(String pw2) { 
                this.pw2 = pw2; 
        }
 
        public String getEmail() { 
                return email; 
        }
  
        public void setEmail(String email) { 
                this.email = email; 
        }
 
        public String getUseridMessage() { 
                return useridMessage; 
        }
  
        public void setUseridMessage(String useridMessage) { 
                this.useridMessage = useridMessage; 
        }
 
        public String getPwMessage() { 
                return pwMessage; 
        }
 

        public void setPwMessage(String pwMessage) { 
                this.pwMessage = pwMessage; 
        }

        public String getEmailMessage() { 
                return emailMessage; 
        }
 
        public void setEmailMessage(String emailMessage) { 
                this.emailMessage = emailMessage; 
        }
 
        public String getNickname() {
                return nickname;
        }
 
        public void setNickname(String nickname) { 
                this.nickname = nickname; 
        }
 

        public String getNicknameMessage() { 
                return nicknameMessage; 
        }
 

        public void setNicknameMessage(String nicknameMessage) { 
                this.nicknameMessage = nicknameMessage; 
        }
 

        public boolean isLogin() { 
                return login; 
        }
 
 
        public void setLogin(boolean login) { 
                this.login = login; 
        }
 
        public String getLoginMessage() { 
                return loginMessage; 
        }
 

 
        public void setLoginMessage(String loginMessage) { 
                this.loginMessage = loginMessage; 
        }
 
}
 
